package com.ewhale.feitengguest.ui.user.mine;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.ewhale.feitengguest.R;
import com.ewhale.feitengguest.presenter.user.mine.ResetSeconePresenter;
import com.ewhale.feitengguest.ui.user.MainActivity;
import com.ewhale.feitengguest.utils.Contants;
import com.ewhale.feitengguest.view.user.mine.ResetSeconeView;
import com.orhanobut.hawk.Hawk;
import com.simga.library.activity.MBaseActivity;
import com.simga.library.utils.AppManager;
import com.simga.library.utils.CheckUtil;
import com.simga.library.utils.HawkKey;
import com.simga.library.utils.StringUtil;
import com.simga.library.widget.BGButton;

import androidx.core.content.ContextCompat;
import butterknife.BindView;
import butterknife.OnClick;
import cn.jiguang.share.android.api.JShareInterface;
import cn.jiguang.share.qqmodel.QQ;
import cn.jiguang.share.wechat.Wechat;

public class ResetPhoneSeconeActivity extends MBaseActivity<ResetSeconePresenter> implements ResetSeconeView {
    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.et_code)
    EditText mEtCode;
    @BindView(R.id.btn_getcode)
    BGButton mBtnGetcode;
    @BindView(R.id.btn_comfir)
    BGButton mBtnComfir;

    private String token;

    public static void open(MBaseActivity context, String token) {
        Bundle bundle = new Bundle();
        bundle.putString("token", token);
        context.startActivity(bundle, ResetPhoneSeconeActivity.class);
    }

    @Override
    protected void onGetBundle(Bundle bundle) {
        token = bundle.getString("token");
    }

    @Override
    protected int getViewId() {
        return R.layout.activity_reset_phone;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setTitle("修改手机号码");
        mBtnComfir.setText("确定");
        mEtPhone.setHint("请输入新的手机号码");
    }

    @Override
    protected boolean isUseBaseTitleBar() {
        return true;
    }

    @Override
    protected void initListener() {
        mEtCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().length() > 0) {
                    mBtnComfir.setNormalSolid(ContextCompat.getColor(mContext, R.color.main_color));
                    mBtnComfir.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                    mBtnComfir.setNormalStroke(1, ContextCompat.getColor(mContext, R.color.main_color));
                    mBtnComfir.setEnabled(true);
                } else {
                    mBtnComfir.setNormalSolid(Color.parseColor("#EEEEEE"));
                    mBtnComfir.setTextColor(Color.parseColor("#BBBBC2"));
                    mBtnComfir.setNormalStroke(1, Color.parseColor("#D4D4D4"));
                    mBtnComfir.setEnabled(false);
                }
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    @Override
    public void showProLoading() {
        showLoading();
    }

    @Override
    public void dimissProLoading() {
        dismissLoading();
    }

    @Override
    public void showErrorMessage(int postCode, String code, String message) {
        showErrorMsg(code, message);
    }

    @OnClick({R.id.btn_getcode, R.id.btn_comfir})
    public void onViewClicked(View view) {
        String phone;
        String code;
        switch (view.getId()) {
            case R.id.btn_getcode:
                phone = mEtPhone.getText().toString();
                if (CheckUtil.isNull(phone)) {
                    showToast("请输入手机号码");
                    return;
                }
                if (!StringUtil.isMobile(phone)) {
                    showToast("请输入正确手机号码");
                    return;
                }
                if (Hawk.get(HawkKey.NOW_APP_STATUS, Contants.USER_STATUS).equals(Contants.SHOP_STATUS)) {
                    presenter.getCode(Hawk.get(HawkKey.SHOP_AUTHENTICATION,""),phone);
                } else {
                    presenter.getCode(Hawk.get(HawkKey.USER_AUTHENTICATION,""),phone);
                }
                break;
            case R.id.btn_comfir:
                code = mEtCode.getText().toString();
                phone = mEtPhone.getText().toString();
                if (Hawk.get(HawkKey.NOW_APP_STATUS, Contants.USER_STATUS).equals(Contants.SHOP_STATUS)) {
                    presenter.resetPhone(2,phone, code, token);
                } else {
                    presenter.resetPhone(1,phone, code, token);
                }
                break;
        }
    }

    private CountDownTimer mTimer = new CountDownTimer(60 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            mBtnGetcode.setText((millisUntilFinished / 1000) + "s");
            mBtnGetcode.setTextColor(ContextCompat.getColor(mContext, R.color.text_666666));
            mBtnGetcode.setEnabled(false);
            mBtnGetcode.setNormalSolid(ContextCompat.getColor(mContext, R.color.default_window_color));
            mBtnGetcode.setNormalStroke(1, ContextCompat.getColor(mContext, R.color.default_window_color));
        }

        @Override
        public void onFinish() {
            mBtnGetcode.setTextColor(ContextCompat.getColor(mContext, R.color.main_color));
            mBtnGetcode.setEnabled(true);
            mBtnGetcode.setNormalSolid(ContextCompat.getColor(mContext, R.color.white));
            mBtnGetcode.setNormalStroke(1, ContextCompat.getColor(mContext, R.color.main_color));
        }
    };

    @Override
    public void getCodeSuccess() {
        showToast("验证码已发送，请注意查收");
        mTimer.start();
    }

    @Override
    public void resetSuccess() {
        showToast("修改成功，请使用新手机登录");
        if(Hawk.get(HawkKey.NOW_APP_STATUS,Contants.USER_STATUS).equals(Contants.SHOP_STATUS)){
            sendBroadcast(new Intent(HawkKey.SHOP_LOGIN_OUT));
        }else{
            sendBroadcast(new Intent(HawkKey.USER_LOGIN_OUT));
        }
    }
}
